package com.wanchuang.hepos.ui.page.home;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.wanchuang.architecture.utils.GsonUtils;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.data.bean.ZhanDanBean;
import com.wanchuang.hepos.bridge.request.HomeRequestViewModel;
import com.wanchuang.hepos.bridge.state.home.ZhanDanDetailViewModel;
import com.wanchuang.hepos.databinding.FragmentZhanDanDetailBinding;
import com.wanchuang.hepos.ui.base.BaseFragment;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class ZhanDanDetailFragment extends BaseFragment {
    private FragmentZhanDanDetailBinding mBinding;
    private ZhanDanDetailViewModel mViewModel;
    private HomeRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(ZhanDanDetailViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(HomeRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zhan_dan_detail, container, false);
        mBinding = FragmentZhanDanDetailBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            showShortToast(s);
        });
        mRequestViewModel.getTradingDetail().observe(this, trading -> {
            mViewModel.bean.set(trading);
            switch (trading.getStatus())
            {
                case "交易成功":
                    mViewModel.visible.set(true);
                    mBinding.tvStatus.setTextColor(Color.GREEN);
                    break;
                case "交易失败":
                case "初始化":
                    mViewModel.visible.set(false);
                    mBinding.tvStatus.setTextColor(Color.RED);
                    break;
                case "已到账":
                    mViewModel.visible.set(true);
                    mBinding.tvStatus.setTextColor(Color.BLUE);
                    break;
            }
        });
        ZhanDanBean zhanDanBean = GsonUtils.jsonToBean(getArguments().getString("data"), ZhanDanBean.class);
        String time = zhanDanBean.getTime();
        String id = zhanDanBean.getId();
        mRequestViewModel.get_trading_detail(id, time);

    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }
    }
}
